package com.nervenets.template.controller.user.params;

import com.nervenets.general.annotation.FormDefinition;
import com.nervenets.general.exception.LogicException;
import com.nervenets.general.web.params.FormParams;
import com.nervenets.template.enumeration.Gender;
import com.nervenets.template.hibernate.domain.user.User;
import com.nervenets.template.service.UserService;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

@EqualsAndHashCode(callSuper = true)
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class UserFormParams extends FormParams<User, UserService> {
    @ApiModelProperty(value = "账号", required = true)
    @FormDefinition(value = "账号", required = true, maxLength = 20)
    @NotBlank
    @Size(min = 5, max = 20)
    private String mobile;
    @ApiModelProperty(value = "密码", required = true)
    @FormDefinition(value = "密码", inputType = FormDefinition.InputType.inputPassword, required = true)
    @NotBlank
    @Size(min = 6, max = 20)
    private String password;
    @ApiModelProperty(value = "昵称", required = true)
    @FormDefinition(value = "昵称", required = true, maxLength = 20)
    @NotBlank
    @Size(min = 2, max = 20)
    private String name;
    @ApiModelProperty(value = "头像")
    @FormDefinition(value = "头像")
    private String avatar;
    @ApiModelProperty(value = "性别")
    @FormDefinition(value = "性别",
            inputType = FormDefinition.InputType.radio,
            selectionService = UserService.class,
            selectionMethod = "genderSelections")
    private Gender gender;
    @ApiModelProperty(value = "是否启用")
    @FormDefinition(value = "是否启用", inputType = FormDefinition.InputType.switchButton)
    private boolean enable;
    @ApiModelProperty(value = "省份")
    @FormDefinition(value = "省份")
    private String province;
    @ApiModelProperty(value = "城市")
    @FormDefinition(value = "城市")
    private String city;

    @Override
    public User validateAndBuild(UserService service, HttpServletRequest request, HttpServletResponse response) {
        throw new LogicException("该接口不提供直接直接添加客户账号，您可以添加管理人员或导师");
        /*User user = new User();
        if (getFormId() > 0) user = service.findOne(getFormId());

        if (service.hasUniqueKey(user.getId(), "mobile", mobile))
            throw new LogicException(400, String.format("【%s】已经存在，请核实。", mobile));

        BeanUtils.copyProperties(this, user, Global.defaultIgnoreProperties());
        return user;*/
    }
}
